package cn.sunlelin.educationserver.service.Impl;

import cn.sunlelin.educationserver.entity.AttendCourse;
import cn.sunlelin.educationserver.mapper.AttendCourseMapper;
import cn.sunlelin.educationserver.service.AttendCourseService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AttendCourseServiceImpl implements AttendCourseService {
    @Autowired
    private AttendCourseMapper attendCourseMapper;
    @Override
    public Boolean createAttendCourse(AttendCourse attendCourse) {
        if(attendCourseMapper.selectAttendCourse(attendCourse)!=null)
            return false;
        attendCourse.setCreateTime(new Date());
        attendCourse.setClickTime(new Date());
        attendCourseMapper.createAttendCourse(attendCourse);
        return true;
    }

    @Override
    public Boolean updateAttendCourse(AttendCourse attendCourse) {
        AttendCourse attendCourseSelect=new AttendCourse();
        attendCourseSelect.setUserId(attendCourse.getUserId());
        attendCourseSelect.setCourseId(attendCourse.getCourseId());
        if(attendCourseMapper.selectAttendCourse(attendCourseSelect)==null)
            return false;
        attendCourse.setUpdateTime(new Date());
        attendCourse.setClickTime(new Date());
        attendCourseMapper.updateAttendCourse(attendCourse);
        return true;
    }

    @Override
    public Map<String, Object> selectAttendCourse(AttendCourse attendCourse) {
        return attendCourseMapper.selectAttendCourse(attendCourse);
    }

    @Override
    public Map<String, Object> selectAttendCourseAndName(AttendCourse attendCourse) {
        return attendCourseMapper.selectAttendCourseAndName(attendCourse);
    }

    @Override
    public List<Map<String,Object>> selectAttendByUserId(Integer userId){
        return attendCourseMapper.selectAttendByUserId(userId);
    }

    @Override
    public Page<Map<String,Object>> getPageCourseByUserId(Integer currentPage, Integer pageSize, Integer userId) {
        PageHelper.startPage(currentPage,pageSize);
        List<Map<String,Object>> courseList=attendCourseMapper.selectCourseByAttendUserId(userId);
        return (Page<Map<String, Object>>) courseList;
    }
    @Override
    public Boolean deleteAttendCourse(AttendCourse attendCourse) {
        if(attendCourseMapper.selectAttendCourse(attendCourse)==null)
            return false;
        attendCourse.setUpdateTime(new Date());
        attendCourseMapper.deleteAttendCourse(attendCourse);
        return true;
    }
}
